package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.common.ShfConstant;
import com.atguigu.entity.UserFollow;
import com.atguigu.entity.UserInfo;
import com.atguigu.entity.vo.UserFollowVo;
import com.atguigu.result.Result;
import com.atguigu.service.UserFollowService;
import com.atguigu.service.UserInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Author: 翟文海
 * @Date: 2022/10/31/031 19:31
 * @Version: 1.0
 * @Description:
 **/
@RestController
@RequestMapping("/userFollow")
public class UserFollowController extends BaseController {
    @Reference
    private UserFollowService userFollowService;

    @GetMapping("/auth/follow/{houseId}")
    public Result addFollow(@PathVariable("houseId") Long houseId, HttpSession session) {
        //拦截器已经把没有登录的拦截下来了
        //1.在session中获取user数据
        UserInfo userInfo = (UserInfo) session.getAttribute(ShfConstant.UserConstant.USER_INFO);
        //2.根据userid和houseid插入数据，关注此房,表示哪个用户关注哪个房子
        UserFollow userFollow = new UserFollow();
        userFollow.setHouseId(houseId);
        userFollow.setUserId(userInfo.getId());
        userFollowService.insert(userFollow);
        return Result.ok();
    }

    //userFollow/auth/list/" + pageNum + "/" + this.page.pageSize
    @GetMapping("/auth/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable Integer pageNum,
                       @PathVariable Integer pageSize,
                       HttpSession session) {
        UserInfo userInfo = (UserInfo) session.getAttribute(ShfConstant.UserConstant.USER_INFO);

        PageInfo<UserFollowVo> page = userFollowService.findListPage(userInfo.getId(), pageNum, pageSize);
        return Result.ok(page);
    }

    @GetMapping("/auth/cancelFollow/{id}")
    public Result cancelFollow(@PathVariable("id") Long id, HttpSession session) {
        UserFollow userFollow = new UserFollow();
        userFollow.setId(id);
        userFollow.setIsDeleted(1);
        userFollowService.update(userFollow);
        return Result.ok();
    }
}